// pages/pay/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headTop: getApp().globalData.headTop,
    // 顶部筛选框部分
    show1: false,
    allParkingLotList: [],
    parkingName: '',
    show2: false,
    allParkingSpace: [],
    parkingSpaceName: '',
    show3: false,
    // 参数
    params: {
      tollCollectorPaymentOrderId: '',
      parkingLotId: '',
      parkingSpaceId: '',
      retroactivelyType: '',
      licensePlatePhotoId: '',
      amount: '',
      plateType: 0,
      disabledPlatePhotoId: [],
      tollCollectorId: '',
      licensePlateNumber: ''
    },
    // 补交类型
    retroactivelyType: [{
        name: '补交',
        id: 0
      },
      {
        name: '逃费',
        id: 1
      }
    ],
    // 二维码部分
    showCode: false,
    codeInfo: {
      tollCollectorPaymentOrderId: 111,
      paymentQRCode: 'https//asdasdasdasd'
    },
    // 未支付订单id
    tollcollectorpaymentorderid: ''
  },
  // 刷新二维码
  getPayQRCodeByTollCollectorOrder() {
    if (this.data.codeInfo.paymentQRCode) {
      this.setData({
        'codeInfo.paymentQRCode': new Date().getTime()
      })
    } else {
      wx.showToast({
        title: '暂无二维码信息',
        icon: 'none'
      })
    }
  },
  // 查询停车费补交或包月支付结果
  getPayResultsByPaymentOrderId() {
    if (this.data.tollcollectorpaymentorderid) {
      return
    }
    if (this.data.codeInfo.paymentQRCode) {
      wx.showModal({
        title: '支付成功',
        content: `订单已被支付,支付金额为${this.data.params.amount}元`,
        showCancel: false,
        complete: (res) => {
          wx.switchTab({
            url: './ispay/index',
          })
        }
      })

    } else {
      wx.showToast({
        title: '暂无二维码信息',
        icon: 'none'
      })
    }
  },
  // 发起补交
  payRetroactivelyQRCode(e) {
    console.log(e)
    let params = this.data.params
    params.amount = e.detail.value.amount
    if (params.plateType == 4 && params.disabledPlatePhotoId.length == '') {
      wx.showToast({
        title: '请上传残疾人信息',
        icon: 'none'
      })
      return
    }
    if (params.amount <= 0) {
      wx.showToast({
        title: '请输入补缴金额',
        icon: 'none'
      })
      return
    }
    if (params.retroactivelyType === '') {
      wx.showToast({
        title: '请选择补交类型',
        icon: 'none'
      })
      return
    }
    if (params.licensePlateNumber === '') {
      wx.showToast({
        title: '请选择补交车牌号',
        icon: 'none'
      })
      return
    }
    params.disabledPlatePhotoId = params.disabledPlatePhotoId.join(',')
    this.setData({
      showCode: true,
      codeInfo: {
        tollCollectorPaymentOrderId: 111,
        paymentQRCode: 'https//asdasdasdasd'
      }
    })
  },
  // 上传残疾人图片
  afterRead2(event) {
    const {
      file
    } = event.detail;
    // 上传完成需要更新 fileList
    let fileList = this.data.params.disabledPlatePhotoId;
    fileList.push({
      ...file
    });
    this.setData({
      'params.disabledPlatePhotoId': fileList
    });
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        const {
          fileList = []
        } = this.data;
        fileList.push({
          ...file,
          url: res.data
        });
        this.setData({
          fileList
        });
      },
    });
  },
  // 切换是否为残疾人
  plateTypeChange(e) {
    console.log(e)
    this.setData({
      'params.plateType': e.detail.value ? 4 : 0
    })
  },
  // 点击补交车牌去拍照
  chooseImage(e) {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res)
        that.setData({
          'params.licensePlateNumber': '苏a12345'
        });
      }
    })
  },
  getallParkingLotList() {
    // 模拟接口查询街道
    this.setData({
      allParkingLotList: [{
        parkingLotId: 100,
        parkingName: '测试停车场'
      }]
    })
  },
  // 选择某街道
  onConfirm(e) {
    console.log(e)
    this.onClose()
    if (e.detail.value.name) {
      // 选择补交类型
      this.setData({
        "params.retroactivelyType": e.detail.value.id
      })
    } else if (e.detail.value.parkingLotId) {
      // 选择街道
      this.setData({
        "params.parkingLotId": e.detail.value.parkingLotId,
        parkingName: e.detail.value.parkingName,
        "params.parkingSpaceId": '',
        parkingSpaceName: ''
      })
      this.getallParkingSpace(e.detail.value.parkingLotId)
    } else if (e.detail.value.parkingSpaceId) {
      // 选择车位
      this.setData({
        "params.parkingSpaceId": e.detail.value.parkingSpaceId,
        parkingSpaceName: e.detail.value.parkingSpaceName
      })
    }

  },
  getallParkingSpace(parkingLotId) {
    // 查询某停车场的所有车位
    let params = {
      parkingLotId
    }
    this.setData({
      allParkingSpace: [{
        parkingSpaceId: 100,
        parkingSpaceName: '测试001车位'
      }]

    })
  },
  // 显示选择街道
  showPopup(e) {
    console.log(e)
    if (this.data.tollcollectorpaymentorderid) {
      return
    }
    const {
      index
    } = e.currentTarget.dataset
    if (index == 2 && this.data.parkingName == '') {
      wx.showToast({
        title: '请先选择街道',
        icon: 'none'
      })
      return
    }
    this.setData({
      [`show${index}`]: true
    });
  },
  // 关闭弹窗
  onClose() {
    this.setData({
      show1: false,
      show2: false,
      show3: false
    });
  },
  // 返回首页
  onClickLeft() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getallParkingLotList()
    // 回显未支付订单id
    this.setData({
      tollcollectorpaymentorderid: getApp().globalData.tollcollectorpaymentorderid || ''
    })
    if (this.data.tollcollectorpaymentorderid) {
      this.getTollCollectorPaymentOrderByIdEcho()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        list: [{
            "pagePath": "/pages/pay/index",
            "text": "补交停车费",
            "iconPath": "/images/tabbar/pay.png",
            "selectedIconPath": "/images/tabbar/paya.png"
          },
          {
            "pagePath": "/pages/pay/nopay/index",
            "text": "未支付订单",
            "iconPath": "/images/tabbar/nopay.png",
            "selectedIconPath": "/images/tabbar/nopaya.png"
          },
          {
            "pagePath": "/pages/pay/ispay/index",
            "text": "已支付订单",
            "iconPath": "/images/tabbar/ispay.png",
            "selectedIconPath": "/images/tabbar/ispaya.png"
          }
        ]
      })
    }
  },
  // 查询未支付订单详情
  getTollCollectorPaymentOrderByIdEcho() {
    const res = {
      parkingLotId: 100,
      parkingName: '承平街',
      parkingSpaceId: 100,
      parkingSpaceName: '承平街001车位',
      retroactivelyType: 1,
      licensePlateNumber: '苏e12345',
      plateType: 0,
      disabledPlatePhotoId: '1,2,3,4',
      amount: 100.00,
      paymentQRCode: 'https://adsfasdfasdf'
    }
    this.setData({
      parkingName: res.parkingName,
      parkingSpaceName: res.parkingSpaceName,
      'params.retroactivelyType': res.retroactivelyType,
      'params.licensePlateNumber': res.licensePlateNumber,
      'params.amount': res.amount * 1,
      'params.plateType': res.plateType,
      'showCode': true,
      'codeInfo.paymentQRCode': res.paymentQRCode,
      'codeInfo.tollCollectorPaymentOrderId': this.data.tollcollectorpaymentorderid
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      showCode: false,
      params: {
        tollCollectorPaymentOrderId: '',
        parkingLotId: '',
        parkingSpaceId: '',
        retroactivelyType: '',
        licensePlatePhotoId: '',
        amount: '',
        plateType: 0,
        disabledPlatePhotoId: [],
        tollCollectorId: '',
        licensePlateNumber: ''
      },
      parkingName: '',
      parkingSpaceName: '',
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})